/*
 * File     : JniNativeMethod.java
 * Created  : 21 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.call;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.type.Pointer;

/**
 * <p>
 *  Encapsulates information about a JNI native method; equivalent to JNI's
 *  <code>JNINativeMethod</code> type.
 * </p>
 *
 * @author Matthew Wilson
 */
public final class JniNativeMethod
{

    private final String name;

    private final String signature;

    private final Pointer functionPointer;

    /**
     * @param name
     *            the name of the method
     * @param signature
     *            the signature of the method
     * @param functionPointer
     *            the function pointer of the method
     */
    public JniNativeMethod( final String name, final String signature, final Pointer functionPointer )
    {
        assert signature.matches( "\\([ZBCSIJFD]*\\)[ZBCSIJFDV]" ) : signature;
        this.name = name;
        this.signature = signature;
        this.functionPointer = functionPointer;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return this.name;
    }

    /**
     * @return the signature
     */
    public String getSignature()
    {
        return this.signature;
    }

    /**
     * @return the function pointer
     */
    public Pointer getFunctionPointer()
    {
        return this.functionPointer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString()
    {
        return "[" + this.name + this.signature + " " + this.functionPointer + "]";
    }

    /**
     * <p>
     *  Registers a native method.
     * </p>
     *
     * @param implClass
     *            the implementing class that owns the method
     */
    public void register( final Class< ? > implClass )
    {
        DniInternal.LOGGER.fine( "Registering native method on " + implClass.getName() + " " + this );
        int result = registerImpl( implClass,
                                   this.name,
                                   this.signature,
                                   this.functionPointer.address() );

        if ( result != 0 )
        {
            throw new InternalError( "Failed to register native method on " + implClass.getName() + " " + this + ": " + result );
        }
    }

    private static native int registerImpl( Class< ? > implClass, String name, String signature, long address );

}
